#!/bin/bash

################################################
#Single flash total_progress:
#00-20:Upload firmware
#21-30:pre-update(Discover MTD device...)
#31-60:Erase firmware
#61-90:Write firmware
#91-99:Verify firmware
#
#Dual flash total_progress:
#00-20:Upload firmware
#21-30:pre-update(Discover MTD device...)
#31-45:Erase firmware1
#46-60:Write firmware1
#61-65:Verify firmware1
#66-80:Erase firmware2
#81-95:Write firmware2
#96-100:Verify firmware2
#################################################

check_cmd_state()
{
    if [ $? -ne 0 ]; then
        echo "$1 Failed..."
        exit 1 ##exit script
    fi
}

###########################################################
#Filter the last occurrence of the specified string,
#remove the parentheses,and set the progress bar variable.
#
#arg1:String to filter
#arg2:percentage of total progress
#
###########################################################
find_str_progress()
{
    str=$(echo "$data" | grep -i $1 |tail -n 1)
    if [ -z "$str" ]; then
        #echo "return  $1"
        return 0
    fi
    str=${str##*(}
    progress=${str%)*}
    if [ -n $progress  ]; then
        total_progress=$(echo $progress $2 $3 $total_progress  |awk '{print int($1*$2*$3+$4)}')
		#echo "[$1] : $progress,$2,$3 = $total_progress" >> /tmp/find_str_progress.log
    fi
    return 0
}

####################
#check_update_state():
#return
#0 : Update
#1 : End Update
####################
check_update_state()
{
    state=$(echo "$data" | grep "end")
    if [ -n "$state" ]; then
        return 1
    fi
    return 0
}

##Set bios update dbus progress interface
## $1=ID $2=Progress
set_dbus_progress_info()
{
    if [ "$set_progress" == $2  ]; then
        return 0
    fi
    set_progress=$2
    FILE=$1
    ID=${FILE##*/}
    OBJ='xyz.openbmc_project.Software.BMC.Updater'
    OBJ_PATH="/xyz/openbmc_project/software/$ID"
    INTERFACE='xyz.openbmc_project.Software.ActivationProgress'
    PROPERTY='Progress'

    busctl set-property  $OBJ $OBJ_PATH $INTERFACE $PROPERTY y $2
    check_cmd_state "Set FW:$ID progress=$2% progress to dbus interface"
    return 0
}

###########################
#
#MAIN
#
###########################
set_progress=0
start_progress=$2
end_progress=$3
ratio=$(echo $start_progress $end_progress |awk '{print int($2-$1)/70}')
while :
do
    #
    ##Get progress data output by flashcp and remove '\r\n' characters.
    #
    data=`cat /tmp/bios_flash_progress | sed  's/\r/\n/g'`

    #
    ##Check if the update is over,exit loop.
    #
    check_update_state
    if [ $? -eq 1 ]; then
        break
    fi

    #
    ##Calculate the progress value of each programming
    ##Initial value equal to 30% or 65%.
    ##contains upload firmware and pre-update phase
    #
    total_progress=$start_progress
    find_str_progress "Erasing" "0.3" "$ratio"
    find_str_progress "Writing" "0.3" "$ratio"
    find_str_progress "Verifying" "0.09" "$ratio"

    ##Write the progress value to the DBUS interface.
    set_dbus_progress_info $1 $total_progress
    sleep 2
done

